/**
 * 
 */
package edu.uwm.nlp.jude.internal;

import java.util.ArrayList;

import edu.uwm.nlp.jude.util.StringUtil;

/**
 * @author qing
 *
 * Jun 16, 2009
 */
public class AuthorFullTextSpot {
	
	private String fullText;
	private ArrayList<ReferenceEntity> rEntList;
	private int spotCount = 0;
	
	public int getSpotCount() {
		return spotCount;
	}
	
	public AuthorFullTextSpot(String fullText, ArrayList<ReferenceEntity> rEntList)
	{
		this.fullText = fullText.toLowerCase();
		this.rEntList = rEntList;
	}
	
	public ArrayList<ReferenceEntity> extractSpot()
	{
		String idStr;
		spotCount = 0;
		ArrayList<String> contexts;
		for(ReferenceEntity entity : rEntList)
		{
			idStr = entity.getIdInRef();
//			System.out.println("\n"+idStr+"--->>");
			if(idStr!=null)
			{
				String[] words = idStr.toLowerCase().split(",");
				if(words.length==2)
				{
					contexts = checkPair(words[0], words[1], 64);
					entity.getContextList().addAll(contexts);
					spotCount+=contexts.size();
				}
			}
		}
//		System.out.println("spotCount="+spotCount);
		return rEntList;
	}
	
	public ArrayList<ReferenceEntity> extractBracketAuthorSpot()
	{
		String idStr;
		for(ReferenceEntity entity : rEntList)
		{
			idStr = entity.getIdInRef();
//			System.out.println(idStr+"--->>");
			entity.getContextList().addAll(scanFulText(idStr));
		}
		return rEntList;
	}
	
	
	private ArrayList<String> scanFulText(String idStr)
	{
		int index = 0;
		int length = fullText.length();
		ArrayList<String> contexts = new ArrayList<String>();
		while(index != -1  && index <length)
		{
			index = fullText.indexOf(idStr, index);
			if(index != -1)
			{
				contexts.add(StringUtil.getContext(fullText, index, index+idStr.length(), 16));
//				System.out.println("\t%-->>"+StringUtil.getContext(fullText, index, index+idStr.length(), 16));
				index++;
			}
		}
		return contexts;
	}
	
	public ArrayList<String> checkPair(String author, String year, int dist)
	{
		ArrayList<String> contexts = new ArrayList<String>();
		int start = fullText.indexOf(author);
		if(start == -1)
		{
			System.err.println("start of author["+author+"]=-1");
			return contexts;
		}
		
		int yearStart = fullText.indexOf(year, start);
		if(yearStart == -1)
		{
			System.err.println("start of year["+year+"]=-1");
			return contexts;
		}
		
		int nearest = fullText.lastIndexOf(author, yearStart);
		start = nearest;
		int end;
		while(start!=-1 && yearStart !=-1)
		{
			nearest = fullText.lastIndexOf(author, yearStart);
			start = nearest;
//			System.out.println(author+"-"+year+"="+(yearStart - start+author.length()));
			if(yearStart - start+author.length() < dist)
			{
				end = yearStart+year.length();
				contexts.add(StringUtil.getContext(fullText, start, end, 15));
//				System.out.println("\t%-->>"+StringUtil.getContext(fullText, start, end, 15));
			}
			else
				System.err.println(author+"-"+year+">"+dist);
			start = fullText.indexOf(author,yearStart+1);
			yearStart = fullText.indexOf(year,start+1);
		}
		
		return contexts;
	}

}
